package com.swak.netty;

import java.io.IOException;

import com.google.common.collect.Maps;
import com.swak.entity.Result;
import com.swak.reactivex.transport.Message;
import com.swak.reactivex.transport.codec.ExchangeCodec;
import com.swak.reactivex.transport.exception.CodecException;
import com.swak.registry.URL;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * 编码的测试 - 编码和解码
 * 
 * @author 618lf
 */
public class MessageCodec {

	public static void main(String[] args) throws IOException, CodecException {
		URL url = new URL("swak", "127.0.0.1", 80, Maps.newHashMap());
		ExchangeCodec codec = new ExchangeCodec(url);
		ByteBuf buffer = Unpooled.buffer(1024);
		buffer.writeByte(1);
		buffer.writeLong(1L);
		Message message = new Message();
		message.addOption(Message.Request).setData(Result.success("我是李锋"));
		codec.encode(null, buffer, message);
		System.out.println("编码结束");
		System.out.println("数据起始：" + buffer.readerIndex());
		message = (Message) codec.decode(null, buffer);
		if (message != null) {
			System.out.println(message.isRequest());
			System.out.println(message.getData());
		} else {
			System.out.println("跳过噪音");
		}
		System.out.println("数据起始：" + buffer.readerIndex());
		message = (Message) codec.decode(null, buffer);
		if (message != null) {
			System.out.println(message.isRequest());
			System.out.println(message.getData());
		}
	}
}
